package imagecutter.amra;

/**
 *
 * @author hudimar
 */
public class Rectangle {

    private int x;
    private int y;
    private int height;
    private int width;
    private String filebasename;
    private String extension;

    public Rectangle(int x, int y, int width, int height){
        this.x = x;
        this.y = y;
        this.height = height;
        this.width = width;
    }

    /**
     * Get the value of height
     *
     * @return the value of height
     */
    public int getHeight() {
        return height;
    }

    /**
     * Get the value of width
     *
     * @return the value of width
     */
    public int getWidth() {
        return width;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public Rectangle getSubRectangle(int verticalImageCount, int horizontalImageCount, int verticalPosition, int horizontalPosition)
            throws IllegalArgumentException
    {
        checkSubRectangleSettings(verticalImageCount, horizontalImageCount, verticalPosition, horizontalPosition);

        RectangleSide rectangleXSide = getRectangleSide(width, verticalImageCount, verticalPosition);
        RectangleSide rectangleYSide = getRectangleSide(height, horizontalImageCount, horizontalPosition);
        Rectangle subRectangle = new Rectangle(
                rectangleXSide.point, rectangleYSide.point,
                rectangleXSide.getLength(), rectangleYSide.getLength()
        );
        subRectangle.setFileBaseName(filebasename+"-"+horizontalPosition+"-"+verticalPosition);
        subRectangle.setFileExtension(extension);
        return subRectangle;
    }

    private RectangleSide getRectangleSide(int length, int count, int position){
        RectangleSide result = null;
        if(count != position+1){
            result = new RectangleSide(position*(length/count), length/count);
        }
        else{
            result = new RectangleSide(position*(length/count), length - position*(length/count));
        }
        return result;
    }
    class RectangleSide{
        int point;
        int length;
        RectangleSide(int point, int lenght){
            this.point = point;
            this.length = lenght;
        }
        public int getLength() {
            return length;
        }
        public int getPoint() {
            return point;
        }

		@Override
		public String toString() {
			return point + "-" + length;
		}
    }

    private void checkSubRectangleSettings(int verticalImageCount, int horizontalImageCount, int verticalPosition, int horizontalPosition)
            throws IllegalArgumentException
    {
        //TODO
    }

    public String getFilename(){
        return filebasename+"."+extension;
    }
    public void setFileBaseName(String filebasename){
        this.filebasename = filebasename;
    }
    public void setFileExtension(String extension){
        this.extension = extension;
    }

}
